
import React from 'react';

import styles from './RuleListAndOption.css';
import {Table, Divider, Tag, message} from 'antd';
import RuleService from "../../business/rule/RuleService";

const rowSelection = {
  onChange: (selectedRowKeys, selectedRows) => {
  },
  getCheckboxProps: record => ({
    disabled: record.name === 'Disabled User',
    name: record.name,
  }),
};

class RuleListAndOption extends React.Component{

  constructor(props) {
    super(props);
    this.state ={
      giftData:[]
    }
    this.ruleService =new RuleService();
    this.callBackQueryList = this.callBackQueryList.bind(this);
    this.disableRule = this.disableRule.bind(this);
    this.enableRule =this.enableRule.bind(this);
    this.callBackEODList =this.callBackEODList.bind(this);

  }
  ruleService:RuleService;
  state ={
    giftData:[]
  }

  columns = [{
    title: '活动唯一标识',
    dataIndex: 'activityId',
    key: 'activityId',
    render: text => <a href="javascript:;">{text}</a>,
  }, {
    title: '活动名称',
    dataIndex: 'activityName',
    key: 'activityName',
  }, {
    title: '积分',
    dataIndex: 'points',
    key: 'points',
  }, {
    title: '限制人群',
    dataIndex: 'limitedGroup',
    key: 'limitedGroup',
  }, {
    title: '联系人',
    dataIndex: 'contactPerson',
    key: 'contactPerson',
  }, {
    title: '主办方',
    dataIndex: 'host',
    key: 'host',
  }, {
    title: '有效期',
    dataIndex: 'effectiveDate',
    key: 'effectiveDate',
    render:(text,record)=>{
      return text +" ~ "+record.expiredDate;
    }
  }, {
    title: '状态',
    dataIndex: 'activeFlag',
    key: 'activeFlag',
    render:(text, record)=>{
      if(text){
        return "有效";
      }else{
        return "无效";
      }
    }
  }, {
    title: '操作',
    dataIndex: 'activeFlag',
    key: 'action',
    render: (text, record) => {
      if(text){
        return (<span>
      <a href="javascript:;" onClick={()=>this.disableRule(record.activityId)}>作废</a>
    </span>);
      }else{
        return (<span>
      <a href="javascript:;" onClick={()=>this.enableRule(record.activityId)}>生效</a>
    </span>);
      }

    },
  }];
  enableRule(activityId:string){
    let token  = sessionStorage.getItem("adminLoginToken");
    this.ruleService.enableRule(activityId,token,this.callBackEODList);
  }
  disableRule(activityId:string){
    let token  = sessionStorage.getItem("adminLoginToken");
    this.ruleService.disableRule(activityId,token,this.callBackEODList);

  }
  callBackEODList(res,isSuccess){
    if(!isSuccess || res.code !="200"){
      message.info(res.message);
    }else{
      this.componentWillMount();
    }
  }
  callBackQueryList(giftList,isSuccess){
    if(!isSuccess || giftList.code !="200"){
      message.info(giftList.message);
    }else{
      this.setState({giftData: giftList.data});
    }
  }

  componentWillMount(){
    this.ruleService.queryList(this.callBackQueryList);
  }



  show(giftData:any[]){
    if(giftData.length !=0){
     return (<Table rowSelection={rowSelection} columns={this.columns} dataSource={giftData} />);
    }else{
      return "";
    }
  }

  render() {
    const {giftData} = this.state;
    return (
      <div className={styles.normal}>
        {this.show(giftData)}
      </div>
    );
  }
}

export default RuleListAndOption;
